{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "gpuType": "A100"
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU",
    "widgets": {
      "application/vnd.jupyter.widget-state+json": {
        "07d6116015074a0abf92d581c10b23a3": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "HBoxModel",
          "model_module_version": "1.5.0",
          "state": {
            "_dom_classes": [],
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "HBoxModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/controls",
            "_view_module_version": "1.5.0",
            "_view_name": "HBoxView",
            "box_style": "",
            "children": [
              "IPY_MODEL_66f78cbff6b246eead8de2987fde5a1b",
              "IPY_MODEL_8b2ae4c62b104ea1a34d2393d16fcce0",
              "IPY_MODEL_45c56383ee9e4d89906a8a991e724ede"
            ],
            "layout": "IPY_MODEL_851ea9ebed554dbeb0635c534833e149"
          }
        },
        "66f78cbff6b246eead8de2987fde5a1b": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "HTMLModel",
          "model_module_version": "1.5.0",
          "state": {
            "_dom_classes": [],
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "HTMLModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/controls",
            "_view_module_version": "1.5.0",
            "_view_name": "HTMLView",
            "description": "",
            "description_tooltip": null,
            "layout": "IPY_MODEL_44f78270d01246079a535eee8690f504",
            "placeholder": "​",
            "style": "IPY_MODEL_84a20e25afe444fdaa3b3fc22cd89007",
            "value": "Loading checkpoint shards: 100%"
          }
        },
        "8b2ae4c62b104ea1a34d2393d16fcce0": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "FloatProgressModel",
          "model_module_version": "1.5.0",
          "state": {
            "_dom_classes": [],
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "FloatProgressModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/controls",
            "_view_module_version": "1.5.0",
            "_view_name": "ProgressView",
            "bar_style": "success",
            "description": "",
            "description_tooltip": null,
            "layout": "IPY_MODEL_2ef7764b9f1b4cdb82371d2ba9843064",
            "max": 6,
            "min": 0,
            "orientation": "horizontal",
            "style": "IPY_MODEL_5800188c4e3a430dbe015023b0d1a76b",
            "value": 6
          }
        },
        "45c56383ee9e4d89906a8a991e724ede": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "HTMLModel",
          "model_module_version": "1.5.0",
          "state": {
            "_dom_classes": [],
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "HTMLModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/controls",
            "_view_module_version": "1.5.0",
            "_view_name": "HTMLView",
            "description": "",
            "description_tooltip": null,
            "layout": "IPY_MODEL_eb510b1b21584e9287a11fd17bae8ee8",
            "placeholder": "​",
            "style": "IPY_MODEL_c87f925ee8aa4b2483fc96b8fa496979",
            "value": " 6/6 [02:15&lt;00:00, 22.51s/it]"
          }
        },
        "851ea9ebed554dbeb0635c534833e149": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "model_module_version": "1.2.0",
          "state": {
            "_model_module": "@jupyter-widgets/base",
            "_model_module_version": "1.2.0",
            "_model_name": "LayoutModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "LayoutView",
            "align_content": null,
            "align_items": null,
            "align_self": null,
            "border": null,
            "bottom": null,
            "display": null,
            "flex": null,
            "flex_flow": null,
            "grid_area": null,
            "grid_auto_columns": null,
            "grid_auto_flow": null,
            "grid_auto_rows": null,
            "grid_column": null,
            "grid_gap": null,
            "grid_row": null,
            "grid_template_areas": null,
            "grid_template_columns": null,
            "grid_template_rows": null,
            "height": null,
            "justify_content": null,
            "justify_items": null,
            "left": null,
            "margin": null,
            "max_height": null,
            "max_width": null,
            "min_height": null,
            "min_width": null,
            "object_fit": null,
            "object_position": null,
            "order": null,
            "overflow": null,
            "overflow_x": null,
            "overflow_y": null,
            "padding": null,
            "right": null,
            "top": null,
            "visibility": null,
            "width": null
          }
        },
        "44f78270d01246079a535eee8690f504": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "model_module_version": "1.2.0",
          "state": {
            "_model_module": "@jupyter-widgets/base",
            "_model_module_version": "1.2.0",
            "_model_name": "LayoutModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "LayoutView",
            "align_content": null,
            "align_items": null,
            "align_self": null,
            "border": null,
            "bottom": null,
            "display": null,
            "flex": null,
            "flex_flow": null,
            "grid_area": null,
            "grid_auto_columns": null,
            "grid_auto_flow": null,
            "grid_auto_rows": null,
            "grid_column": null,
            "grid_gap": null,
            "grid_row": null,
            "grid_template_areas": null,
            "grid_template_columns": null,
            "grid_template_rows": null,
            "height": null,
            "justify_content": null,
            "justify_items": null,
            "left": null,
            "margin": null,
            "max_height": null,
            "max_width": null,
            "min_height": null,
            "min_width": null,
            "object_fit": null,
            "object_position": null,
            "order": null,
            "overflow": null,
            "overflow_x": null,
            "overflow_y": null,
            "padding": null,
            "right": null,
            "top": null,
            "visibility": null,
            "width": null
          }
        },
        "84a20e25afe444fdaa3b3fc22cd89007": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "DescriptionStyleModel",
          "model_module_version": "1.5.0",
          "state": {
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "DescriptionStyleModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "StyleView",
            "description_width": ""
          }
        },
        "2ef7764b9f1b4cdb82371d2ba9843064": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "model_module_version": "1.2.0",
          "state": {
            "_model_module": "@jupyter-widgets/base",
            "_model_module_version": "1.2.0",
            "_model_name": "LayoutModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "LayoutView",
            "align_content": null,
            "align_items": null,
            "align_self": null,
            "border": null,
            "bottom": null,
            "display": null,
            "flex": null,
            "flex_flow": null,
            "grid_area": null,
            "grid_auto_columns": null,
            "grid_auto_flow": null,
            "grid_auto_rows": null,
            "grid_column": null,
            "grid_gap": null,
            "grid_row": null,
            "grid_template_areas": null,
            "grid_template_columns": null,
            "grid_template_rows": null,
            "height": null,
            "justify_content": null,
            "justify_items": null,
            "left": null,
            "margin": null,
            "max_height": null,
            "max_width": null,
            "min_height": null,
            "min_width": null,
            "object_fit": null,
            "object_position": null,
            "order": null,
            "overflow": null,
            "overflow_x": null,
            "overflow_y": null,
            "padding": null,
            "right": null,
            "top": null,
            "visibility": null,
            "width": null
          }
        },
        "5800188c4e3a430dbe015023b0d1a76b": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "ProgressStyleModel",
          "model_module_version": "1.5.0",
          "state": {
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "ProgressStyleModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "StyleView",
            "bar_color": null,
            "description_width": ""
          }
        },
        "eb510b1b21584e9287a11fd17bae8ee8": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "model_module_version": "1.2.0",
          "state": {
            "_model_module": "@jupyter-widgets/base",
            "_model_module_version": "1.2.0",
            "_model_name": "LayoutModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "LayoutView",
            "align_content": null,
            "align_items": null,
            "align_self": null,
            "border": null,
            "bottom": null,
            "display": null,
            "flex": null,
            "flex_flow": null,
            "grid_area": null,
            "grid_auto_columns": null,
            "grid_auto_flow": null,
            "grid_auto_rows": null,
            "grid_column": null,
            "grid_gap": null,
            "grid_row": null,
            "grid_template_areas": null,
            "grid_template_columns": null,
            "grid_template_rows": null,
            "height": null,
            "justify_content": null,
            "justify_items": null,
            "left": null,
            "margin": null,
            "max_height": null,
            "max_width": null,
            "min_height": null,
            "min_width": null,
            "object_fit": null,
            "object_position": null,
            "order": null,
            "overflow": null,
            "overflow_x": null,
            "overflow_y": null,
            "padding": null,
            "right": null,
            "top": null,
            "visibility": null,
            "width": null
          }
        },
        "c87f925ee8aa4b2483fc96b8fa496979": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "DescriptionStyleModel",
          "model_module_version": "1.5.0",
          "state": {
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "DescriptionStyleModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "StyleView",
            "description_width": ""
          }
        }
      }
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "import transformers\n",
        "\n",
        "pipeline = transformers.pipeline(\n",
        "    \"text-generation\",\n",
        "    model=\"microsoft/phi-4\",\n",
        "    model_kwargs={\"torch_dtype\": \"auto\"},\n",
        "    device_map=\"auto\",\n",
        ")\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 190,
          "referenced_widgets": [
            "07d6116015074a0abf92d581c10b23a3",
            "66f78cbff6b246eead8de2987fde5a1b",
            "8b2ae4c62b104ea1a34d2393d16fcce0",
            "45c56383ee9e4d89906a8a991e724ede",
            "851ea9ebed554dbeb0635c534833e149",
            "44f78270d01246079a535eee8690f504",
            "84a20e25afe444fdaa3b3fc22cd89007",
            "2ef7764b9f1b4cdb82371d2ba9843064",
            "5800188c4e3a430dbe015023b0d1a76b",
            "eb510b1b21584e9287a11fd17bae8ee8",
            "c87f925ee8aa4b2483fc96b8fa496979"
          ]
        },
        "id": "YkThFIx5Pnot",
        "outputId": "94cf5586-f1c9-4e14-b949-ffdbad665663"
      },
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_auth.py:94: UserWarning: \n",
            "The secret `HF_TOKEN` does not exist in your Colab secrets.\n",
            "To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.\n",
            "You will be able to reuse this secret in all of your notebooks.\n",
            "Please note that authentication is recommended but still optional to access public models or datasets.\n",
            "  warnings.warn(\n"
          ]
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "Loading checkpoint shards:   0%|          | 0/6 [00:00<?, ?it/s]"
            ],
            "application/vnd.jupyter.widget-view+json": {
              "version_major": 2,
              "version_minor": 0,
              "model_id": "07d6116015074a0abf92d581c10b23a3"
            }
          },
          "metadata": {}
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "Device set to use cuda:0\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "with open('/content/question.txt', 'r') as f:\n",
        "  question = f.read()"
      ],
      "metadata": {
        "id": "nn7mmTgoQ5_B"
      },
      "execution_count": 3,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "questions = question.splitlines()"
      ],
      "metadata": {
        "id": "rSKwoNYWQnjO"
      },
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "data = []"
      ],
      "metadata": {
        "id": "JT1P-Pp1Qs1u"
      },
      "execution_count": 5,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "messages = [\n",
        "    {\"role\": \"system\", \"content\": \"You are a java coder\"},\n",
        "    {\"role\": \"user\", \"content\": \"使用Java实现快速排序?\"},\n",
        "]\n",
        "\n",
        "outputs = pipeline(messages, max_new_tokens=1000)\n",
        "print(outputs[0][\"generated_text\"][-1])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Rsrn_BFSPplz",
        "outputId": "3ca78b64-c2c5-4a1d-c25a-eb1c2d29d7c6"
      },
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{'role': 'assistant', 'content': '当然！快速排序是一种高效的排序算法，使用分治法来对数组进行排序。它的平均时间复杂度为 \\\\(O(n \\\\log n)\\\\)，但最坏情况下为 \\\\(O(n^2)\\\\)。以下是使用Java实现快速排序的示例代码：\\n\\n```java\\npublic class QuickSort {\\n\\n    // 快速排序的主方法\\n    public static void quickSort(int[] array, int low, int high) {\\n        if (low < high) {\\n            // 获取分区点\\n            int pivotIndex = partition(array, low, high);\\n            // 递归对左半部分进行排序\\n            quickSort(array, low, pivotIndex - 1);\\n            // 递归对右半部分进行排序\\n            quickSort(array, pivotIndex + 1, high);\\n        }\\n    }\\n\\n    // 分区函数\\n    private static int partition(int[] array, int low, int high) {\\n        // 选择最后一个元素作为枢轴\\n        int pivot = array[high];\\n        int i = low - 1; // 小于枢轴的元素的最后一个位置\\n\\n        for (int j = low; j < high; j++) {\\n            // 如果当前元素小于枢轴，交换位置\\n            if (array[j] < pivot) {\\n                i++;\\n                swap(array, i, j);\\n            }\\n        }\\n        // 将枢轴元素放到正确的位置\\n        swap(array, i + 1, high);\\n        return i + 1; // 返回枢轴的最终位置\\n    }\\n\\n    // 交换数组中两个元素的方法\\n    private static void swap(int[] array, int i, int j) {\\n        int temp = array[i];\\n        array[i] = array[j];\\n        array[j] = temp;\\n    }\\n\\n    // 测试快速排序\\n    public static void main(String[] args) {\\n        int[] array = {10, 7, 8, 9, 1, 5};\\n        System.out.println(\"排序前的数组:\");\\n        printArray(array);\\n\\n        quickSort(array, 0, array.length - 1);\\n\\n        System.out.println(\"排序后的数组:\");\\n        printArray(array);\\n    }\\n\\n    // 打印数组的方法\\n    private static void printArray(int[] array) {\\n        for (int value : array) {\\n            System.out.print(value + \" \");\\n        }\\n        System.out.println();\\n    }\\n}\\n```\\n\\n### 代码解释：\\n\\n1. **`quickSort` 方法**：这是快速排序的主要递归方法。它接受数组、起始索引和结束索引作为参数。如果起始索引小于结束索引，它会调用 `partition` 方法来获取分区点，然后递归地对左右两部分进行排序。\\n\\n2. **`partition` 方法**：这个方法选择最后一个元素作为枢轴，并将小于枢轴的元素移动到它的左边，大于枢轴的元素移动到它的右边。最后，它将枢轴元素放到正确的位置，并返回枢轴的最终位置。\\n\\n3. **`swap` 方法**：这是一个辅助方法，用于交换数组中两个元素的位置。\\n\\n4. **`printArray` 方法**：用于打印数组的内容，方便查看排序前后的结果。\\n\\n5. **`main` 方法**：用于测试快速排序算法，展示排序前后的数组。\\n\\n这个实现使用了最后一个元素作为枢轴，但你也可以选择其他策略，如随机选择枢轴或使用三数取中法来提高性能。'}\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "i = 0"
      ],
      "metadata": {
        "id": "kk9cdhjAVOu1"
      },
      "execution_count": 1,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "for question in questions:\n",
        "  print(f\"question : {i}\")\n",
        "  i += 1\n",
        "  messages = [{\"role\": \"system\", \"content\": \"You are a java coder\"},{\"role\": \"user\", \"content\": question},]\n",
        "  outputs = pipeline(messages, max_new_tokens=512)\n",
        "  data.append({\"question\":question, \"answer\":outputs[0][\"generated_text\"][-1]})"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "D-HLhSSuVKwq",
        "outputId": "c78ab106-970b-46b9-9f41-344df6fa1f4a"
      },
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "question : 0\n",
            "question : 1\n",
            "question : 2\n",
            "question : 3\n",
            "question : 4\n",
            "question : 5\n",
            "question : 6\n",
            "question : 7\n",
            "question : 8\n",
            "question : 9\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "You seem to be using the pipelines sequentially on GPU. In order to maximize efficiency please use a dataset\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "question : 10\n",
            "question : 11\n",
            "question : 12\n",
            "question : 13\n",
            "question : 14\n",
            "question : 15\n",
            "question : 16\n",
            "question : 17\n",
            "question : 18\n",
            "question : 19\n",
            "question : 20\n",
            "question : 21\n",
            "question : 22\n",
            "question : 23\n",
            "question : 24\n",
            "question : 25\n",
            "question : 26\n",
            "question : 27\n",
            "question : 28\n",
            "question : 29\n",
            "question : 30\n",
            "question : 31\n",
            "question : 32\n",
            "question : 33\n",
            "question : 34\n",
            "question : 35\n",
            "question : 36\n",
            "question : 37\n",
            "question : 38\n",
            "question : 39\n",
            "question : 40\n",
            "question : 41\n",
            "question : 42\n",
            "question : 43\n",
            "question : 44\n",
            "question : 45\n",
            "question : 46\n",
            "question : 47\n",
            "question : 48\n",
            "question : 49\n",
            "question : 50\n",
            "question : 51\n",
            "question : 52\n",
            "question : 53\n",
            "question : 54\n",
            "question : 55\n",
            "question : 56\n",
            "question : 57\n",
            "question : 58\n",
            "question : 59\n",
            "question : 60\n",
            "question : 61\n",
            "question : 62\n",
            "question : 63\n",
            "question : 64\n",
            "question : 65\n",
            "question : 66\n",
            "question : 67\n",
            "question : 68\n",
            "question : 69\n",
            "question : 70\n",
            "question : 71\n",
            "question : 72\n",
            "question : 73\n",
            "question : 74\n",
            "question : 75\n",
            "question : 76\n",
            "question : 77\n",
            "question : 78\n",
            "question : 79\n",
            "question : 80\n",
            "question : 81\n",
            "question : 82\n",
            "question : 83\n",
            "question : 84\n",
            "question : 85\n",
            "question : 86\n",
            "question : 87\n",
            "question : 88\n",
            "question : 89\n",
            "question : 90\n",
            "question : 91\n",
            "question : 92\n",
            "question : 93\n",
            "question : 94\n",
            "question : 95\n",
            "question : 96\n",
            "question : 97\n",
            "question : 98\n",
            "question : 99\n",
            "question : 100\n",
            "question : 101\n",
            "question : 102\n",
            "question : 103\n",
            "question : 104\n",
            "question : 105\n",
            "question : 106\n",
            "question : 107\n",
            "question : 108\n",
            "question : 109\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import json\n",
        "with open(\"data.json\", \"w\", encoding=\"utf-8\") as f:\n",
        "    json.dump(data, f, ensure_ascii=False, indent=4)  # indent=4 让 JSON 更易读"
      ],
      "metadata": {
        "id": "7KymifJ4RBuo"
      },
      "execution_count": 7,
      "outputs": []
    }
  ]
}